from werkzeug.security import generate_password_hash,check_password_hash
from flask_login import UserMixin,AnonymousUserMixin
from itsdangerous import TimedJSONWebSignatureSerializer as Serializer
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from datetime import datetime
# from app import db
import pymysql
pymysql.install_as_MySQLdb()


app = Flask( __name__ )
##app.config['SQLALCHEMY_DATABASE_URI'] = "postgresql://postgres:postgres@120.24.93.65 :5432/test"
app.config['SQLALCHEMY_DATABASE_URI'] = "mysql://root:123456@10.211.55.9 :3306/blog"
#app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True

app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
db = SQLAlchemy( app )


# import db
class Role(db.Model):
    __tablename__='roles'
    id=db.Column(db.Integer,primary_key=True)
    name=db.Column(db.String(64),unique=True)
    default =db.Column(db.Boolean,default=False,index=True)
    permissions=db.Column(db.Integer)
    users=db.relationship('User',backref='role',lazy='dynamic')

#权限常量
class Permission:
    FOLLOW=0x01
    COMMENT=0x02
    WRITE_ARTICLES=0x04
    MODERATE_COMMENTS=0x08
    ADMINISTER=0x08

class User(UserMixin,db.Model):
    __tablename__='users'
    id=db.Column(db.Integer,primary_key=True)
    email=db.Column(db.String(64),unique=True,index=True)
    username=db.Column(db.String(64),unique=True,index=True)
    password_hash=db.Column(db.String(128))
    role_id=db.Column(db.Integer,db.ForeignKey('roles.id'))
    confirmed=db.Column(db.Boolean,default=False)
    name = db.Column(db.String(64))   #真实姓名
    location = db.Column(db.String(64))   #所在地
    about_me = db.Column(db.Text())   #自我介绍
    member_since = db.Column(db.DateTime(),default=datetime.utcnow)   #注册日期
    last_seen = db.Column(db.DateTime(),default=datetime.utcnow)    #最后访问日期

class AnonymousUser(AnonymousUserMixin):
    def can(self,permissions):
        return False
    def is_administrator(self):
        return False


